Skip to content

Add support for saving 16-bit images to pnm formats #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2025

Conversation

Enet4
Copy link
Contributor

@Enet4 Enet4 commented Feb 27, 2025

Should hopefully resolve #2430, as well as #1687. It takes the assumption that when an image is 16-bit and the encoder receives a &[u8], it means that the samples were in reality 16-bit samples in native endianness. An aligned copy of the input is made if the slice isn't 2-byte aligned.

Summary

  • add sample adaptation layer which recovers 16-bit samples from the original image buffer depending on the color type provided
    • Once this is done, the number of samples should match the expected number of samples, and the rest of the encoding process should simply work
  • allow for RGB and RGB Alpha tuple types in header check
  • add test suite save_pnm.rs and test sample images from pngsuite:
    • basi0g16.png: 16-bit grayscale image
    • basn2c16.png: 16-bit RGB image

Known caveat

  • I am not entirely sure whether the assumption can be made in general, or whether it would be possible for someone to request 8-bit samples to be extended and encoded as 16-bit through the encoder.
  • While this should also enable the encoder to write PAM files, AFAIK PAM file decoding is not yet supported in the library.

@Enet4 Enet4 marked this pull request as ready for review February 27, 2025 22:19
@fintelia
Copy link
Contributor

fintelia commented Mar 1, 2025

Thanks for this PR! I am in favor of adding 16-bit support here.

One concern is that the write_image should not require that users pass aligned slices. If the color type is a 16-bit per pixel format then the user is allowed to pass any native endian pixel data without worrying about alignment. One option to handle potentially misaligned inputs is to allocate a temporary Vec<u16> and copy the provided data into it. That adds some memory overhead, but should be pretty fast to implement.

@Enet4
Copy link
Contributor Author

Enet4 commented Mar 1, 2025

One option to handle potentially misaligned inputs is to allocate a temporary Vec<u16> and copy the provided data into it. That adds some memory overhead, but should be pretty fast to implement.

I'll see what I can do. The encoder implementation cannot switch the lifetime of the samples midway, so the best place to re-adapt the byte buffer (with an allocation if necessary) would be as early as possible in the impl.

@Enet4
Copy link
Contributor Author

Enet4 commented Jun 26, 2025

This has been rebased and the previous concerns have been addressed, so it is ready for another review. I cannot inspect the logs of the failing CI checks.

@fintelia
Copy link
Contributor

Looks good to me. Not quite sure why GitHub didn't rerun the CI tests when you pushed again

Enet4 added 3 commits June 26, 2025 20:37
- add sample adaptation layer which
  recovers 16-bit samples from the original image buffer
  depending on the color type provided
- allow for RGB and RGB Alpha tuple types in header check
- add test suite save_pnm and test sample images from pngsuite:
   - basi0g16.png: grayscale 16-bit
   - basn2c16.png: rgb 16-bit
- move wide sample reinterpretation to the public function call
- reallocate image input if samples are not aligned properly
- refactor a bit of  `encode` into `encode_impl` to reduce repetition
@fintelia fintelia force-pushed the bug/2430-save-pnm branch from ccc6655 to cd0c8de Compare June 27, 2025 03:41
@fintelia fintelia merged commit c83941b into image-rs:main Jun 27, 2025
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot save 16-bit images directly to PNM via save
2 participants